'use strict';

const conn = require('../../libs/Conn');

module.exports = async function (params) {
    let projectTypeSql = ``;
    let orderBySql = `ORDER BY ef.engineering_no ASC`;
    let buildingUseSql = ``;
    let engineeringStatusSql = ``;
    let searchValueSql = ``;
    let startDateSql = ``;
    let userSql = ``;

    /*排序*/
    if(params.sort&&params.dir){
        switch (params.sort) {
            case 'engineering_no':
                orderBySql = `ORDER BY ef.engineering_no ${params.dir}`;
                break;
            case 'engineering_name':
                orderBySql = `ORDER BY ef.engineering_name ${params.dir}`;
                break;
            case 'building_use':
                orderBySql = `ORDER BY bf.building_name ${params.dir}`;
                break;
            case 'startDate':
                orderBySql = `ORDER BY ef.startDate ${params.dir}`;
                break;
            case 'region_name':
                orderBySql = `ORDER BY rf.region_name ${params.dir}`;
                break;
            case 'user_name':
                orderBySql = `ORDER BY uf.user_name ${params.dir}`;
                break;
            case 'status':
                orderBySql = `ORDER BY ef.status ${params.dir}`;
                break;
            case 'structure_type':
                orderBySql = `ORDER BY sf.structure_name ${params.dir}`;
                break;
        }
    }
    /*分页*/
    let pageSql = `LIMIT ${params.pageNum>1?((params.pageNum-1)*10):0},10;SELECT FOUND_ROWS() AS count`;
    /*项目类型筛选*/
    if(params.projectType){
        projectTypeSql = ``;
    }
    /*建筑用途筛选*/
    if(params.buildingUse){
        if(params.buildingUse!=='0'){
            buildingUseSql = `AND ef.building_id = '${params.buildingUse}' `
        }
    }
    /*工程状态筛选*/
    if(params.engineeringStatus){
        engineeringStatusSql = `AND ef.status = '${params.engineeringStatus}' `
    }
    /*工程地点、名称筛选*/
    if(params.searchValue){
        searchValueSql = `AND (rf.region_name LIKE '%${params.searchValue}%' OR ef.engineering_name LIKE '%${params.searchValue}%') `
    }
    /*时间筛选*/
    if(params.startDateA && params.startDateB){
        startDateSql = `AND ef.startDate BETWEEN '${params.startDateA}' AND '${params.startDateB}' `
    }

    /*角色判断*/
    if(params.is_manager !==1){
        userSql = `AND ef.engineering_id IN (               SELECT
                                                                engineering_id
                                                            FROM
                                                                engineering_file
                                                            WHERE
                                                                principal_id = '${params.user_id}'
                                                            AND delete_sign = 1
                                                            UNION
                                                                SELECT
                                                                    efa.engineering_id
                                                                FROM
                                                                    engineering_user_middle eum
                                                                INNER JOIN engineering_file efa ON efa.engineering_id = eum.engineering_id
                                                                AND efa.delete_sign = 1
                                                                WHERE
                                                                    eum.user_id = '${params.user_id}'
                                                            ) `;
    }

    let querySql = `SELECT SQL_CALC_FOUND_ROWS
                            ef.engineering_id,
                            ef.engineering_name,
                            ef.engineering_no,
                            DATE_FORMAT(ef.startDate, '%Y-%m-%d') AS startDate,
                            rf.region_id,
                            rf.region_name,
                            bf.building_id,
                            bf.building_name AS building_use,
                            sf.structure_id,
                            sf.structure_name AS structure_type,
                            ef.principal_id AS user_id,
                            ef.status,
                            uf.user_name
                        FROM
                            engineering_file ef
                        LEFT JOIN user_file uf ON ef.principal_id = uf.user_id AND uf.delete_sign = 1
                        LEFT JOIN region_file rf ON ef.region_id = rf.region_id AND rf.delete_sign = 1 AND rf.enable = 1
                        LEFT JOIN building_file bf ON ef.building_id = bf.building_id AND bf.delete_sign = 1 AND bf.enable = 1
                        LEFT JOIN structure_file sf ON ef.structure_id = sf.structure_id AND sf.delete_sign = 1 AND sf.enable = 1
                        WHERE
	ef.delete_sign = 1 ${userSql} ${orderBySql} ${projectTypeSql} ${buildingUseSql} ${engineeringStatusSql} ${searchValueSql} ${startDateSql} ${pageSql}`;
    let rec = await conn(querySql);
    for(let data in rec[0] ){
        switch (rec[0][data].status) {
            case 1:
                rec[0][data].status = '<span style="color: #ff3800">在建</span>';
                break;
            case 2:
                rec[0][data].status = '<span style="color: #ff3800">竣工未验收</span>';
                break;
            case 3:
                rec[0][data].status = '<span style="color: #ff3800">已验收未备案</span>';
                break;
            case 4:
                rec[0][data].status = '<span style="color: #2672d9">已验收且备案</span>';
                break;
        }
    }
    let pageNum = Math.ceil(rec[1][0].count/10);
    if(rec[0].length>0){
        rec[0][0].dataTotal = rec[1][0].count;
        rec[0][0].pageTotal = pageNum;
        rec[0][0].pageNum = params.pageNum;
    }
    return {success: true, data: rec[0]};

};